package com.junt.library.adapter;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.junt.library.R;
import com.junt.library.model.Media;
import com.junt.library.widgets.CheckView;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.AudioViewHolder> {
    private Context context;

    /**
     * 列表数据
     */
    private List<Media> medias;
    /**
     * 已选择的音频集合
     */
    private List<Media> selectedMedias;

    private SimpleDateFormat dateFormat;

    public AudioAdapter(Context context) {
        this.context = context;
        medias = new ArrayList<>();
        selectedMedias = new ArrayList<>();

        dateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    }

    @NonNull
    @Override
    public AudioViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AudioViewHolder(LayoutInflater.from(context).inflate(R.layout.item_audio, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AudioViewHolder holder, int position) {
        final Media media = medias.get(position);
        holder.tvName.setText(media.getName());
        holder.tvInfo.setText(String.format("%s - %s - %s", getDate(media.getDateModified()), getDuration(media.getDuration()), getSize(media.getSize())));
        holder.checkBox.setOnCheckedChangeListener(new CheckView.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CheckView buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedMedias.add(media);
                } else {
                    selectedMedias.remove(media);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return medias.size();
    }

    public List<Media> getSelectedMedias() {
        return selectedMedias;
    }

    public void add(Media media) {
        medias.add(media);
        notifyItemInserted(medias.size() - 1);
    }

    public void setNewData(List<Media> newData) {
        if (equalList(medias,newData)){
            return;
        }
        medias.clear();
        medias.addAll(newData);
        notifyDataSetChanged();
    }

    public boolean equalList(List<Media> list1, List<Media> list2) {
        return (list1.size() == list2.size()) && list1.containsAll(list2);
    }

    class AudioViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvInfo;
        private CheckView checkBox;

        public AudioViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvInfo = itemView.findViewById(R.id.tvInfo);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }

    /**
     * 时间戳转日期字符串
     *
     * @param date 时间戳
     * @return 2020/02/03
     */
    private String getDate(long date) {
        return dateFormat.format(date * 1000);
    }

    /**
     * 时长转时分秒
     *
     * @param duration 毫秒
     * @return 10:02:00
     */
    private String getDuration(long duration) {
        int hours = (int) (duration / (1000 * 60 * 60));
        int min = (int) ((duration - hours * (1000 * 60 * 60)) / (1000 * 60));
        int seconds = (int) ((duration - hours * (1000 * 60 * 60) - min * (1000 * 60)) / 1000);

        return String.format("%s:%s:%s",
                hours < 10 ? "0" + hours : hours,
                min < 10 ? "0" + min : min,
                seconds < 10 ? "0" + seconds : seconds);
    }

    /**
     * 文件尺寸（b）转文件尺寸（mb）
     *
     * @param size 字节数
     * @return 0.05MB
     */
    private String getSize(long size) {
        return String.format(Locale.CHINA, "%.2fMB", (float) size / 1000 / 1000);
    }

}
